package com.ruoyi.web.controller.report;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.report.vo.*;
import com.ruoyi.system.domain.vo.GetPdtdVo;
import com.ruoyi.system.service.report.ITeacherService;
import com.ruoyi.web.controller.api.dto.GetSequenceDto;
import io.swagger.annotations.ApiOperation;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/report/teacher")
public class TeacherReportController extends BaseController {

    @Autowired
    private ITeacherService iTeacherService;

    @Autowired
    private TokenService tokenService;

    /**
     * 分页条件查询
     *
     * @param tabPtdetailTailPatientVO
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "患者分页条件查询")
    public AjaxResult list(@RequestBody TabPtdetailTailPatientVO tabPtdetailTailPatientVO) {
        System.out.println(tabPtdetailTailPatientVO);
        return AjaxResult.success(iTeacherService.getList(tabPtdetailTailPatientVO));
    }

    /**
     * 检查项目下拉框
     *
     * @return
     */
    @GetMapping("/getDropDownCheckName")
    @ApiOperation(value = "获取下拉检查项目")
    public AjaxResult getDropDownCheckName() {
        List<String> list = iTeacherService.getDropDownCheckName();
        return AjaxResult.success(list);
    }


    /**
     * 下拉状态
     *
     * @return
     */
    @GetMapping("/getDropDownState")
    @ApiOperation(value = "获取下拉状态")
    public AjaxResult getDropDownState() {
        return AjaxResult.success(iTeacherService.getDropDownState());
    }

    /**
     * 获取患者随访记录
     *
     * @param ptdeId
     * @return
     */
    @GetMapping("/getSurvey/{ptdeId}")
    @ApiOperation(value = "获取随访记录")
    public AjaxResult getSurvey(@PathVariable String ptdeId) {
        return AjaxResult.success(iTeacherService.getSurvey(ptdeId));
    }


    /**
     * 查询病史
     *
     * @param ptdeId
     * @return
     */
    @GetMapping("/getBgReadyTxt/{ptdeId}")
    public AjaxResult getBgReadyTxt(@PathVariable String ptdeId, String reportId) {
        String userName = tokenService.getLoginUser(ServletUtils.getRequest()).getUsername();
        return AjaxResult.success(iTeacherService.getBgReadyTxt(ptdeId, userName, reportId));
    }

    /**
     * 检查意见
     *
     * @param patientId
     * @return
     */
    //@GetMapping("/getCheckyjTxt/{patientId}")
    public AjaxResult ptdtCheckyjTxt(@PathVariable String patientId) {
        return AjaxResult.success(iTeacherService.ptdtCheckyjTxt(patientId));
    }


    /**
     * 检查所见
     *
     * @param patientId
     * @return
     */
    //@GetMapping("/getCheckSjTxt/{patientId}")
    public AjaxResult ptdtCheckSjTxt(@PathVariable String patientId) {
        return AjaxResult.success(iTeacherService.ptdtCheckSjTxt(patientId));
    }


    /**
     * 患者查询
     *
     * @param
     * @return
     */
    @GetMapping("/getPatient/{ptdeId}")
    @ApiOperation(value = "学生患者查询")
    public AjaxResult getPatient(@PathVariable String ptdeId) {
        return AjaxResult.success(iTeacherService.getPatient(ptdeId));
    }

    /**
     * 更新患者信息
     *
     * @param ptPatientVo
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新患者信息")
    public AjaxResult updatePatient(@RequestBody PtPatientVo ptPatientVo) {
        iTeacherService.updatePatient(ptPatientVo);
        try {

        } catch (Exception e) {
            throw new GlobalException("保存失败");
        }
        return AjaxResult.success("保存成功", null);
    }


    /**
     * 获取历史记录
     *
     * @param
     * @return
     */
    @GetMapping("/getHistory/{patientId}/{ptdeId}")
    @ApiOperation(value = "获取历史记录")
    public AjaxResult getHistory(@PathVariable String patientId, @PathVariable String ptdeId) {
        return AjaxResult.success(iTeacherService.getHistory(patientId, ptdeId));
    }


    /**
     * 获取 病例,检查意见,检查所见
     *
     * @param
     * @return
     */
    @GetMapping("/patientReport/{ptdeId}")
    @ApiOperation(value = "获取 病史,检查意见,检查所见")
    public AjaxResult getPatientReportInfo(@PathVariable String ptdeId) {
        return AjaxResult.success("rows", iTeacherService.getPatientReportInfo(ptdeId));
    }

    /**
     * 序列
     *
     * @param checkCode
     * @return
     */
    @GetMapping("/series/{checkCode}")
    @ApiOperation(value = "序列")
    public AjaxResult getStudiesSeries(@PathVariable String checkCode) {
        return AjaxResult.success(iTeacherService.getStudiesSeries(checkCode));
    }


    /**
     * 查询模板
     *
     * @return
     */
    @GetMapping("/template")
    @ApiOperation(value = "模板")
    public AjaxResult getTemplate() {
        Long userId = tokenService.getLoginUser(ServletUtils.getRequest()).getUserId();
        return AjaxResult.success("publicLibrary", iTeacherService.getTemplate(userId));
    }

    /**
     * 检查意见检查所见模板
     *
     * @param id
     * @param parentId
     * @return
     */
    @GetMapping("/templateInfo/{id}/{parentId}")
    @ApiOperation(value = "模板")
    public AjaxResult templateInfo(@PathVariable String id, @PathVariable String parentId) {
        if (id == null || parentId == null) {
            return AjaxResult.error("查询失败");
        }
        return AjaxResult.success(iTeacherService.templateInfo(id, parentId));
    }


    /**
     * 发布患者
     *
     * @param
     * @return
     */
    @PostMapping("/release")
    public AjaxResult addRelease(@RequestBody List<ClassStudentListVo> classStudentListVo) {
        String teacherUserName = tokenService.getLoginUser(ServletUtils.getRequest()).getUsername();
        try {
            iTeacherService.addRelease(classStudentListVo, teacherUserName);
        } catch (Exception e) {
            throw new GlobalException("发布异常,请联系管理员");
        }
        return AjaxResult.success("发布成功");
    }

    /**
     * 获取当前用户管理的所有班级
     *
     * @return
     */
    @GetMapping("/classList")
    public AjaxResult getClassList() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return AjaxResult.success(iTeacherService.getClassList(loginUser.getUsername()));
    }

    /**
     * 查询班级内的所有学生
     *
     * @param classZ
     * @return
     */
    @GetMapping("/classStudentList/{class}")
    public AjaxResult getClassStudentList(@PathVariable("class") String classZ) {
        return AjaxResult.success(iTeacherService.getClassStudentList(classZ));
    }


    /**
     * 学生报告条件分页查询
     *
     * @param studentReportReqVo
     * @return
     */
    @PostMapping("/studentReportListPage")
    public TableDataInfo getStudentReportListPage(@RequestBody StudentReportReqVo studentReportReqVo) {
        String userName = tokenService.getLoginUser(ServletUtils.getRequest()).getUsername();
        startPage();
        List<StudentReportListPageResVo> studentReportListPage = iTeacherService.getStudentReportListPage(studentReportReqVo, userName);
        return getDataTable(studentReportListPage);
    }

    /**
     * 提交评价
     *
     * @param commentScoreReqVo
     * @return
     */
    @PostMapping("/score/{status}")
    public AjaxResult addCommentScore(@RequestBody CommentScoreReqVo commentScoreReqVo, @PathVariable String status) {
        if (StringUtils.isEmpty(commentScoreReqVo.getScore())) {
            throw new GlobalException("分数不能为空");
        }
        iTeacherService.addCommentScore(commentScoreReqVo, status);
        return AjaxResult.success("保存成功");
    }

    /**
     * 提交评价
     *
     * @param commentScoreReqVo
     * @return
     */
    @PostMapping("update/score")
    public AjaxResult updateCommentScore(@RequestBody CommentScoreReqVo commentScoreReqVo) {
        if (StringUtils.isEmpty(commentScoreReqVo.getScore())) {
            throw new GlobalException("分数不能为空");
        }
        iTeacherService.updateCommentScore(commentScoreReqVo);
        return AjaxResult.success("保存成功");
    }

    /**
     * 获取报告完成状态
     *
     * @param
     * @return
     */
    @GetMapping("completeStatus")
    public AjaxResult getCompleteStatus() {
        return AjaxResult.success(iTeacherService.getCompleteStatus());
    }


    /**
     * 评价学生报告
     *
     * @return
     */
    @GetMapping("commentReport/{reportId}")
    public AjaxResult getReportComment(@PathVariable String reportId) {
        return AjaxResult.success(iTeacherService.getReportComment(reportId));
    }

    /**
     * 获取所有检查类型
     *
     * @return
     */
    @GetMapping("/getDropDownCheckMachine")
    public AjaxResult getDropDownCheckMachine() {
        List<String> list = iTeacherService.getDropDownCheckMachine();
        return AjaxResult.success(list);
    }

    /**
     * 获取所有检查设备
     *
     * @return
     */
    @GetMapping("/getDropDownModelName")
    public AjaxResult getDropDownModelName() {
        List<String> list = iTeacherService.getDropDownModelName();
        return AjaxResult.success(list);
    }

    /**
     * 获取所有检查设备（在ptdetail表中查询）
     *
     * @return
     */
    @GetMapping("/getDropDownPtdtModelName")
    public AjaxResult getDropDownPtdtModelName() {
        List<String> list = iTeacherService.getDropDownPtdtModelName();
        return AjaxResult.success(list);
    }


    /**
     * 获取所有显像剂
     *
     * @return
     */
    @GetMapping("/getDropDownXxj")
    public AjaxResult getDropDownXxj() {
        List<String> list = iTeacherService.getDropDownXxj();
        return AjaxResult.success(list);
    }


    /**
     * 通过检查号来获取历史记录
     *
     * @param ptdtCheckCode
     * @return
     */
    @GetMapping("/getHistoryByCheckCode/{ptdtCheckCode}")
    @ApiOperation(value = "通过检查号获取历史记录")
    public AjaxResult getHistoryByCheckCode(@PathVariable String ptdtCheckCode) {
        return AjaxResult.success(iTeacherService.getHistoryByCheckCode(ptdtCheckCode));
    }


    /**
     * 通过患者姓名获取历史记录
     *
     * @param ptName
     * @return
     */
    @GetMapping("/getHistoryByPtName/{ptName}")
    @ApiOperation(value = "通过患者姓名获取历史记录")
    public AjaxResult getHistoryByName(@PathVariable String ptName) {
        return AjaxResult.success(iTeacherService.getHistoryByPtName(ptName));
    }

    /**
     * 更详细的患者查询
     *
     * @param ptdeId
     * @return
     */
    @GetMapping("/getPatientPro/{ptdeId}")
    @ApiOperation(value = "更详细的学生患者查询")
    public AjaxResult getPatientPro(@PathVariable String ptdeId) {
        return AjaxResult.success(iTeacherService.getPatientPro(ptdeId));
    }


    /**
     * 更新模板信息
     *
     * @param template
     * @return
     */
    @PutMapping("/updateMyTemplate")
    @ApiOperation(value = "更新模板信息")
    public AjaxResult updateMyTemplate(@RequestBody TemplateUpdata template) {
        Long userId = tokenService.getLoginUser(ServletUtils.getRequest()).getUserId();
        template.setUserId(userId);
        iTeacherService.updateMyTemplate(template);
        try {

        } catch (Exception e) {
            throw new GlobalException("保存失败");
        }
        return AjaxResult.success("保存成功", null);
    }

    /**
     * 新增个人模板
     *
     * @param template
     * @return
     */
    @PostMapping("/addMyTemplate")
    @ApiOperation(value = "新增模板信息")
    public AjaxResult addMyTemplate(@RequestBody TemplateUpdata template) {
        Long userId = tokenService.getLoginUser(ServletUtils.getRequest()).getUserId();
        template.setUserId(userId);
        iTeacherService.addMyTemplate(template);
        try {

        } catch (Exception e) {
            throw new GlobalException("保存失败");
        }
        return AjaxResult.success("保存成功", null);
    }

    /**
     * 删除模板信息
     *
     * @param template
     * @return
     */
    @PostMapping("/delMyTemplate")
    @ApiOperation(value = "删除模板信息")
    public AjaxResult delMyTemplate(@RequestBody TemplateUpdata template) {
        Long userId = tokenService.getLoginUser(ServletUtils.getRequest()).getUserId();
        template.setUserId(userId);

        iTeacherService.delMyChildrenTemplate(template.getChildren());
        iTeacherService.delMyTemplate(template);
        try {

        } catch (Exception e) {
            throw new GlobalException("保存失败");
        }
        return AjaxResult.success("保存成功", null);
    }

    /*
      获取所有设备名称信息
     */
    @GetMapping("/getDropDownUnitType")
    @ApiOperation(value = "获取所有设备名称信息")
    public AjaxResult GetEquipments() {
        List<String> equipments = iTeacherService.GetEquipments();
        return AjaxResult.success("Success", equipments);
    }

    /*
    获取所有院区列表
     */
    @GetMapping("/getBranch")
    @ApiOperation(value = "获取所有院区")
    public AjaxResult GetBranch() {
        List<String> list = iTeacherService.GetBranch();
        return AjaxResult.success("Success", list);
    }

    /*
     获取某用户的报告医生
     */
    @GetMapping("/getBgDoctor")
    @ApiOperation(value = "获取某用户的报告医生")
    public AjaxResult GetBgDoctor(@RequestBody TabPtdetailTailPatientVO tabPtdetailTailPatientVO) {
        List<TabPtdetailTailPatientVO> list = iTeacherService.GetBgDoctor(tabPtdetailTailPatientVO.getPtName());
        return AjaxResult.success("Success", list);
    }

    /*
    获取所有医生
     */
    @GetMapping("/getDoctorDropDown")
    @ApiOperation(value = "获取所有医生")
    public AjaxResult GetDoctorDropDown() {
        List<String> list = iTeacherService.getDoctorDropDown();
        return AjaxResult.success("success", list);
    }

    /*
    更改”患者状态,报告医生，审核医生，复审医生”信息
     */
    @PostMapping("/changePtInfo")
    @ApiOperation(value = "更更改”患者状态,报告医生，审核医生，复审医生”信息")
    public AjaxResult ChangePtInfo(@RequestBody TabPtdetailTailPatientVO tabPtdetailTailPatientVO) {
        iTeacherService.changePtInfo(tabPtdetailTailPatientVO);
        return AjaxResult.success("success");
    }

    /**
     * 报告医生
     *
     * @param bgDto
     * @return
     */
    // 报告提取
    @PostMapping("/ExtractReport")
    public AjaxResult ExtractReport(@RequestBody BgDto bgDto) {
        return iTeacherService.ExtractReport(bgDto);
    }

    // 撤销报告提取
    @PostMapping("/returnPtdStatus")
    public AjaxResult returnPtdStatus(@RequestBody BgDto bgDto) {
        return iTeacherService.returnPtdStatus(bgDto);
    }

    // 获取所有报告医生
    @GetMapping("/AllBgDoctor")
    public AjaxResult AllBgDoctor() {
        return AjaxResult.success(iTeacherService.AllBgDoctor());
    }

    // 提交审核
    @PostMapping("/submitCommited")
    public AjaxResult submitCommited(@RequestBody BgDto bgDto) {
        return iTeacherService.submitCommited(bgDto);
    }

    // 撤销提交审核
    @PostMapping("/backCommited")
    public AjaxResult backCommited(@RequestBody BgDto bgDto) {
        return iTeacherService.backCommited(bgDto);
    }


    /**
     * 审核医生
     *
     * @param bgDto
     * @return
     */
    // 审核提取
    @PostMapping("/ExtractSHReport")
    public AjaxResult ExtractSHReport(@RequestBody BgDto bgDto) {
        return iTeacherService.ExtractSHReport(bgDto);
    }

    // 撤销审核提取
    @PostMapping("/returnPtdStatus2")
    public AjaxResult returnPtdStatus2(@RequestBody BgDto bgDto) {
        return iTeacherService.returnPtdStatus2(bgDto);
    }

    // 审核通过
    @PostMapping("/ReportApproved")
    public AjaxResult ReportApproved(@RequestBody BgDto bgDto) {
        return iTeacherService.ReportApproved(bgDto);
    }

    // 获取所有审核医生
    @GetMapping("/AllShDoctor")
    public AjaxResult AllShDoctor() {
        return AjaxResult.success(iTeacherService.AllShDoctor());
    }

    // 转审
    @PostMapping("/changeShDoctor")
    public AjaxResult changeShDoctor(@RequestBody BgDto bgDto) {
        return iTeacherService.changeShDoctor(bgDto);
    }

    // 提交复审
    @PostMapping("/submitCommited2")
    public AjaxResult submitCommited2(@RequestBody BgDto bgDto) {
        return iTeacherService.submitCommited2(bgDto);
    }

    // 撤销提交复审
    @PostMapping("/backCommited2")
    public AjaxResult backCommited2(@RequestBody BgDto bgDto) {
        return iTeacherService.backCommited2(bgDto);
    }

    /**
     * 复审医生
     *
     * @param bgDto
     * @return
     */
    // 复审提取
    @PostMapping("/changePtdStatus3")
    public AjaxResult changePtdStatus3(@RequestBody BgDto bgDto) {
        return iTeacherService.changePtdStatus3(bgDto);
    }


    // 撤销复审提取
    @PostMapping("/returnPtdStatus3")
    public AjaxResult returnPtdStatus3(@RequestBody BgDto bgDto) {
        return iTeacherService.returnPtdStatus3(bgDto);
    }

    // 复审通过
    @PostMapping("/reportApproved2")
    public AjaxResult reportApproved2(@RequestBody BgDto bgDto) {
        return iTeacherService.reportApproved2(bgDto);
    }

    // 获取所有复审医生
    @GetMapping("/AllFsDoctor")
    public AjaxResult AllFsDoctor() {
        return AjaxResult.success(iTeacherService.AllFsDoctor());
    }

    // 转复审
    @PostMapping("/changeShDoctor2")
    public AjaxResult changeShDoctor2(@RequestBody BgDto bgDto) {
        return iTeacherService.changeShDoctor2(bgDto);
    }

    //获取当前报告状态以及对应医生和时间
    @GetMapping("/timeAndDoctor/{id}")
    public AjaxResult timeAndDoctor(@PathVariable String id) {
        return AjaxResult.success(iTeacherService.timeAndDoctor(id));
    }

    //获取序列
    @PostMapping("/getSequenceByname")
    public AjaxResult getSequenceByname(@RequestBody GetSequenceDto getSequenceDto) {
        return AjaxResult.success(iTeacherService.getSequenceByname(getSequenceDto.getNameList()));
    }

    //获取序列
    @PostMapping("/getSequenceByptdeid")
    public AjaxResult getSequenceByptdeid(@RequestBody GetSequenceDto getSequenceDto) {
        return AjaxResult.success(iTeacherService.getSequenceByptdeid(getSequenceDto.getPtdeid()));
    }

}




